package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.utils.LanguageUtiles;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

public class X3HomeTimeSelectDialog extends X3BaseBottomSheetDialog implements View.OnClickListener {

    private TextView tv_title;
    private TextView tv_cancel;
    private TextView tv_affirm;
    private WheelView<String> wv_year;
    private WheelView<String> wv_month;

    private int currentYear;
    private int currentMonth;
    private X3MarqueeText tv_notice;
    private String sn;

    private List<Integer> monList;
    private List<String> currentMonthList;
    private List<String> zhMonthList;
    private List<String> zhCurrentMonthList;
    private boolean zhRCN;
    private boolean isDe;

    public X3HomeTimeSelectDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    public void initView() {
        tv_title = findViewById(R.id.tv_title);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_affirm = findViewById(R.id.tv_affirm);
        wv_year = findViewById(R.id.wv_year);
        wv_month = findViewById(R.id.wv_month);
        tv_notice = findViewById(R.id.tv_notice);

        tv_cancel.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);

        Calendar calendar = Calendar.getInstance();
        currentYear = calendar.get(Calendar.YEAR);
        currentMonth = calendar.get(Calendar.MONTH) + 1;

        int selectYear = 0;
        List<String> yearList = new ArrayList<>();
        for (int i = 0; i <= (currentYear - 1990); i++) {
            yearList.add((1990 + i) + "");
            if ((1990 + i) == 2010) {
                selectYear = i;
            }
        }

        monList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            monList.add((i + 1));
        }
        zhRCN = LanguageUtiles.isZhRCN();
        isDe = LanguageUtiles.isDe();

        List<String> monthList;
        if (zhRCN) {
            monthList = Arrays.asList(mContext.getResources().getStringArray(R.array.select_month_zh));
        }else {
            if (isDe) {
                monthList = Arrays.asList(mContext.getResources().getStringArray(R.array.select_month_de));
            }else {
                monthList = Arrays.asList(mContext.getResources().getStringArray(R.array.select_month_uk));
            }
        }

        zhMonthList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            if (i < 9) {
                zhMonthList.add("0" + i);
            }else {
                zhMonthList.add(i + "");
            }
        }

        zhCurrentMonthList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            if (i <= currentMonth) {
                if (i < 10) {
                    zhCurrentMonthList.add("0" + i);
                }else {
                    zhCurrentMonthList.add(i + "");
                }
            }
        }

        currentMonthList = new ArrayList<>();
        for (int i = 1; i <= 12; i++)  {
            if (i <= currentMonth) {
                currentMonthList.add(monthList.get(i - 1));
            }
        }

        wv_year.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_year.setSoundEffectResource(R.raw.x3_button_choose);
        wv_year.setSoundEffect(true);
        wv_year.setPlayVolume(0.5f);
        wv_year.setData(yearList);
        wv_year.setSelectedItemPosition(selectYear);
        wv_year.setOnItemSelectedListener((wheelView, data, position) -> {
            if (!X3StringUtils.isEmpty(data)) {
                if (Integer.parseInt(data) == currentYear) {
                    if (zhRCN) {
                        wv_month.setData(zhCurrentMonthList);
                    }else {
                        wv_month.setData(currentMonthList);
                    }

                }else {
                    wv_month.setData(monthList);
                }
            }
        });

        wv_month.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_month.setSoundEffectResource(R.raw.x3_button_choose);
        wv_month.setSoundEffect(true);
        wv_month.setPlayVolume(0.5f);
        wv_month.setData(monthList);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_home_time_number_picker;
    }

    /**
     * 设置选中的月份
     * @param year
     * @param month
     */
    public void setSelect(int year,int month,String title) {
        List<String> data = wv_year.getData();
        if (year == 0) {
            wv_year.setSelectedItemPosition(data.indexOf("2012"));
        }else {
            int yearIndex = 0;
            for (int i = 0; i < data.size(); i++) {
                if (data.get(i).equals(year + "")) {
                    yearIndex = i;
                }
            }
            wv_year.setSelectedItemPosition(yearIndex);
        }

        int monthIndex = 0;
        for (int i = 0; i < monList.size(); i++) {
            if (monList.get(i).equals(month)) {
                monthIndex = i;
            }
        }
        wv_month.setSelectedItemPosition(monthIndex);

        tv_title.setText(title + "");

    }

    public void setNewSelect(int year,String month) {
        List<String> data = wv_year.getData();
        int yearIndex = 0;
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).equals(year + "")) {
                yearIndex = i;
            }
        }
        wv_year.setSelectedItemPosition(yearIndex);

        int i1 = zhMonthList.indexOf(month);
        if (i1 != -1) {
            wv_month.setSelectedItemPosition(i1);
        }
    }

    @Override
    public void show() {
        super.show();
        zhRCN = LanguageUtiles.isZhRCN();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.tv_affirm) {
            dismiss();
            if (iOnClick != null) {
                int selectedItemPosition = wv_month.getSelectedItemPosition();
                List<String> data = wv_month.getData();
                if (data.size() == 12) {
                    iOnClick.iOnClickSelect(wv_year.getSelectedItemData(), wv_month.getSelectedItemData(), zhMonthList.get(selectedItemPosition));
                } else {
                    iOnClick.iOnClickSelect(wv_year.getSelectedItemData(), wv_month.getSelectedItemData(), zhCurrentMonthList.get(selectedItemPosition));
                }

            }
            if (iOnClickTyreYear != null) {
                iOnClickTyreYear.iOnClickTyreYearGet(sn, wv_year.getSelectedItemData(), wv_month.getSelectedItemData());
            }
        }
    }

    /**
     * 展示参考值
     * @param referNotice
     */
    public void showReferNotice(String referNotice) {
        if (X3StringUtils.isEmpty(referNotice)) {
            tv_notice.setVisibility(View.INVISIBLE);
        }else {
            tv_notice.setVisibility(View.VISIBLE);
            tv_notice.setText(String.format(mContext.getString(R.string.x3_refer_notice),referNotice));
        }
    }

    public void setShowSn(String sn) {
        this.sn = sn;
    }

    IOnClickCar iOnClick;

    public void setiOnClick(IOnClickCar iOnClick) {
        this.iOnClick = iOnClick;
    }

    public interface IOnClickCar {
        void iOnClickSelect(String year,String month,String zhMonth);
    }

    IOnClickTyreYear iOnClickTyreYear;

    public void setiOnClickTyreYear(IOnClickTyreYear iOnClickTyreYear) {
        this.iOnClickTyreYear = iOnClickTyreYear;
    }

    public interface IOnClickTyreYear {
        void iOnClickTyreYearGet(String sn,String year,String month);
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("出厂时间选择弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("出厂时间选择弹窗");
    }
}
